package main.Parallel;

import main.initiate.Configuration;

public class ThreadingManager {
    private static ThreadingManager singleton;

    public static ThreadingManager getSingleton() {
        if (singleton != null)
            return singleton;

        singleton = new ThreadingManager();
        return singleton;
    }

    private OperationQueueManager jobQueues;
    private ThreadingManager() {
        this.jobQueues = new OperationQueueManager(getCores());
    }

    /**
     * Equivalent to Configuration.getNumberCores()
     * 
     * @return
     */
    public int getCores() {
        return Configuration.getSingleton().getNumberCores();
    }
    
    public <E> void runThreadedIteration(Iterable<E> iterable,
            final IterationOperation<E> op, int length) {
        
        int index = 0;
        for(E value : iterable) {
            jobQueues.addJob(new IterationOperationJob<E>(op, index, value));
            index++;
        }
    }
}
